/*
 * The LevelDB-CLI Project
 * Copyright (C) 2024 JuRuoqwq<BitPasta@163.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "parse_input.h"

#include "format_input.h"

std::vector<Token> ParseInput::GetToken(const std::string input) const
{
    std::vector<Token> result;
    FormatInput formatInput;
    std::vector<std::string> splited = formatInput.SplitInput(input);

    // 遍历向量一个个比较
    for (const auto &element : splited) {
        if (element == "set") {
            result.push_back({SET, element});
        } else if (element == "get") {
            result.push_back({GET, element});
        } else if (element == "delete" || element == "del") {
            result.push_back({DELETE, element});
        } else if (element == "show") {
            result.push_back({SHOW, element});
        } else if (element == "close") {
            result.push_back({CLOSE, element});
        } else if (element == "clear") {
            result.push_back({CLEAR, element});
        } else if (element == "options" || element == "opts") {
            result.push_back({OPTIONS, element});
        } else if (element == "read_options" || element == "ropts") {
            result.push_back({READ_OPTIONS, element});
        } else if (element == "write_options" || element == "wopts") {
            result.push_back({WRITE_OPTIONS, element});
        } else if (element == "create_if_missing") {
            result.push_back({CREATE_IF_MISSING, element});
        } else if (element == "error_if_exists") {
            result.push_back({ERROR_IF_EXISTS, element});
        } else if (element == "paranoid_checks") {
            result.push_back({PARANOID_CHECKS, element});
        } else if (element == "write_buffer_size") {
            result.push_back({WRITE_BUFFER_SIZE, element});
        } else if (element == "max_open_files") {
            result.push_back({MAX_OPEN_FILES, element});
        } else if (element == "block_size") {
            result.push_back({BLOCK_SIZE, element});
        } else if (element == "block_restart_interval") {
            result.push_back({BLOCK_RESTART_INTERVAL, element});
        } else if (element == "max_file_size") {
            result.push_back({MAX_FILE_SIZE, element});
        } else if (element == "zstd_compression_level") {
            result.push_back({ZSTD_COMPRESSION_LEVEL, element});
        } else if (element == "reuse_logs") {
            result.push_back({REUSE_LOGS, element});
        } else if (element == "verify_checksums") {
            result.push_back({VERIFY_CHECKSUMS, element});
        } else if (element == "fill_cache") {
            result.push_back({FILL_CACHE, element});
        } else if (element == "sync") {
            result.push_back({SYNC, element});
        } else if (element == "help") {
            result.push_back({HELP, element});
        } else if (element == "version") {
            result.push_back({VERSION, element});
        } else if (element == "quit") {
            result.push_back({QUIT, element});
        } else if (element == "open") {
            result.push_back({OPEN, element});
        } else {
            // 无法匹配，是数据
            result.push_back({PARAM, element});
        }
    }
    return result;
}


int ParseInput::ToValue(const Token value) const
{
    if (value.source == "true") {
        return 1;
    } else if (value.source == "false") {
        return 0;
    }
    // 通过stoi捕捉异常
    try {
        return std::stoi(value.source.c_str());
    } catch (const std::exception &e) {
        return INT32_MIN;
    }
}